//
//  Icon_Input_Cell.m
//  CMVRFForIOS
//
//  Created by HailongHan on 15/4/8.
//  Copyright (c) 2015年 hvacapp. All rights reserved.
//

#define INPUT_CELL_HEIGHT 44

#import "Icon_Input_Cell.h"
@interface Icon_Input_Cell()

@property (nonatomic,strong) UIImageView *mIcon;
@property (nonatomic,strong) UITextField *mTextField;

@end

@implementation Icon_Input_Cell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.contentView.backgroundColor = [UIColor whiteColor];
        
        //TODO
        self.mIcon = [[UIImageView alloc] init];
        [self.contentView addSubview:self.mIcon];
        
        self.mTextField = [[UITextField alloc] init];
        self.mTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
        [self.contentView addSubview:self.mTextField];
        
        [self.mTextField addTarget:self action:@selector(textValueChange:) forControlEvents:UIControlEventEditingChanged];
        
        [self.mTextField addTarget:self action:@selector(textValueEnd:) forControlEvents:UIControlEventEditingDidEnd];
        
        [RACObserve(self.mTextField, text) subscribeNext:^(NSString * text) {
            if (self.editDidEndBlock) {
                self.editDidEndBlock(text);
            }
        }];
    }
    return self;
}

-(void)configPlaceHolder:(NSString *)placeHolder andContent:(NSString *)content andIcon:(UIImage *)icon andIsSecure:(BOOL)secure{
    
    
    //是否是密码
    self.mTextField.secureTextEntry = secure;
    self.mTextField.placeholder = placeHolder;
    [self.mTextField setValue:CM_PLACEHOLDER_TEXT_COLOR forKeyPath:@"_placeholderLabel.textColor"];
    [self.mTextField setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];
    if ([content isNotEmpty]) {
        self.mTextField.text = content;
    }
    
    if (icon) {
        self.mIcon.image = icon;
        
        self.mIcon.height = INPUT_CELL_HEIGHT-15;
        self.mIcon.width = INPUT_CELL_HEIGHT-15;
        self.mIcon.x = 10;
        self.mIcon.centerY = INPUT_CELL_HEIGHT*0.5;
    }else{
        self.mIcon.width = 0;
        self.mIcon.height = 0;
        self.mIcon.x = 0;
        self.mIcon.y = 0;
    }
    
    
    self.mTextField.x = CGRectGetMaxX(self.mIcon.frame)+10;
    self.mTextField.centerY = 0;
    self.mTextField.height = INPUT_CELL_HEIGHT;
    self.mTextField.width = kScreen_Width-self.mTextField.x-10;
}

- (void)configKeyboardType:(UIKeyboardType)type {
    self.mTextField.keyboardType = type;
}

- (void)setTextValue:(NSString *)text {
    self.mTextField.text = text;
}

- (void)textValueChange:(UITextField *)textField{
    if (self.textValueChangeBlock) {
        self.textValueChangeBlock(textField.text);
    }
}

- (void)textValueEnd:(UITextField *)textField {
    if (self.editDidEndBlock) {
        self.editDidEndBlock(textField.text);
    }
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
